package controllers

import (
	"myerp/models"
	"myerp/src"
	"time"
)

//商品管理
type GoodsController struct {
	CommonController
}

//添加
func (c *GoodsController) Add() {
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		reData["status"] = 1
		var name, spec, gmodel, units string
		var sorteid, status, salestatus int
		var price float64
		var md models.Goods
		var err error

		if name = c.GetString("name"); name == "" {
			reData["msg"] = "产品名字不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Name = name

		if spec = c.GetString("spec"); spec == "" {
			reData["msg"] = "规格不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Spec = spec
		if gmodel = c.GetString("gmodel"); gmodel == "" {
			reData["msg"] = "型号不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Gmodel = gmodel
		if units = c.GetString("units"); units == "" {
			reData["msg"] = "单位不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Units = units
		if sorteid, err = c.GetInt("sorteid"); err != nil {
			reData["msg"] = "请选择分类"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.SorteId = sorteid
		status, err = c.GetInt("status", 1)
		md.Status = status
		salestatus, err = c.GetInt("salestatus", 0)
		md.SaleStatus = salestatus
		md.Cuid = int(UserInfo.Id)
		if price, err = c.GetFloat("price"); err != nil {
			reData["msg"] = "产品价格必填"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Gcode = src.OrderNumber("G")
		md.Ctime = int(time.Now().Unix())
		md.Price = price

		_, err = models.AddGoods(&md)
		if err != nil {
			reData["msg"] = "添加商品失败！"
		} else {
			reData["url"] = "/goods/list"
			reData["status"] = 1
		}
		c.Data["json"] = reData
		c.ServeJSON()
	}
	//获取以及分类S
	l, _ := models.GetAllSortsByPid(0)
	c.Data["ActionName"] = "添加产品"
	c.Data["ol"] = l

	tl, _ := models.GetAllSortsByPid(l[0].Id)
	c.Data["tl"] = tl
	c.Layout = "base.html"
	c.TplName = "goods/add.html"
}

//编辑
func (c *GoodsController) Edit() {
	var id int
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		reData["status"] = 0
		var md models.Goods
		id, err := c.GetInt("id")
		if err != nil {
			reData["msg"] = "修改的分类不存在！"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Id = id
		var name, spec, gmodel, units string
		var sorteid, status, salestatus int
		var price float64

		if name = c.GetString("name"); name == "" {
			reData["msg"] = "产品名字不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Name = name

		if spec = c.GetString("spec"); spec == "" {
			reData["msg"] = "规格不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Spec = spec
		if gmodel = c.GetString("gmodel"); gmodel == "" {
			reData["msg"] = "型号不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Gmodel = gmodel
		if units = c.GetString("name"); units == "" {
			reData["msg"] = "单位不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Units = units
		if sorteid, err = c.GetInt("sorteid"); err != nil {
			reData["msg"] = "请选择分类"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.SorteId = sorteid
		status, err = c.GetInt("status", 1)
		md.Status = status
		salestatus, err = c.GetInt("salestatus", 0)
		md.SaleStatus = salestatus
		md.Cuid = int(UserInfo.Id)
		if price, err = c.GetFloat("price"); err != nil {
			reData["msg"] = "产品价格必填"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		md.Gcode = src.OrderNumber("G")
		md.Ctime = int(time.Now().Unix())
		md.Price = price

		err = models.UpdateGoodsById(&md)
		if err != nil {
			reData["msg"] = "修改商品失败"
		} else {
			reData["status"] = 1
			reData["url"] = "/goods/list"
		}
		c.Data["json"] = reData
		c.ServeJSON()
		return
	}

	id, err := c.GetInt("id")
	if err != nil {
		c.Redirect("/goods/list", 302)
		return
	}
	sd, err := models.GetGoodsById(id)
	if err != nil {
		c.Redirect("/goods/list", 302)
		return
	}
	//获取分类信息
	one, _ := models.GetSortsById(sd.SorteId)
	c.Data["one"] = one
	//获取二级分类
	two, _ := models.GetAllSortsByPid(one.Pid)
	c.Data["two"] = two
	//获取一级
	l, _ := models.GetAllSortsByPid(0)
	c.Data["ol"] = l
	c.Data["Sdata"] = sd
	c.Layout = "base.html"
	c.TplName = "goods/edite.html"
}

//列表
func (c *GoodsController) List() {
	//稍后添加具体列表
	page, _ := c.GetInt("page", 1)
	count, l := models.GetGoodsList(int64(page), 10, "")
	c.Data["List"] = l
	c.Data["ActionName"] = "产品列表"
	var pages src.Page = src.NewPage(page, 10, int(count), "/goods/list")
	c.Data["page"] = pages.Show()
	c.Layout = "base.html"
	c.TplName = "goods/list.html"
}

//添加各种订单选择商品
func (c *GoodsController) Orlist() {
	//获取订单类型
	ot := c.GetString("ot")
	if ot == "" {
		c.Redirect("main/index", 302)
		return
	}
	//获取商品列表
	if c.Ctx.Request.Method == "POST" {
		c.Redirect(ot+"/add", 302)
		return
	}
	page, _ := c.GetInt("page", 1)
	count, l := models.GetGoodsList(int64(page), 10, "")
	c.Data["List"] = l
	c.Data["ActionName"] = "产品列表"
	var pages src.Page = src.NewPage(page, 10, int(count), "/goods/list")
	c.Data["page"] = pages.Show()
	c.Data["ot"] = ot
	c.Data["ActionName"] = "选择商品"
	c.Layout = "base.html"
	c.TplName = "goods/orlist.html"
}

//ajax获取商品
func (c *GoodsController) Ajaxlist() {
	reData := make(map[string]interface{})
	reData["status"] = 0
	reData["msg"] = "没有找到更多数据！"
	page, _ := c.GetInt("page", 1)
	w := c.GetString("w")
	var where, url string
	if w != "" {
		where = "my_goods.name like '%" + w + "%'"
		url = "/goods/ajaxlist?w=" + w
	} else {
		where = ""
		url = "/goods/ajaxlist"
	}
	count, l := models.GetGoodsList(int64(page), 10, where)
	reData["List"] = l
	c.Data["ActionName"] = "产品列表"
	var pages src.Page = src.NewPage(page, 10, int(count), url)
	reData["page"] = pages.Show()
	c.Data["json"] = reData
	c.ServeJSON()
}

//ajax写商品 ID信息
func (c *GoodsController) Ajaxsession() {
	// 类型
	//id字符串
	ot := c.GetString("ot")
	id := c.GetString("id")
	c.SetSession(ot+"order", id)
	c.ServeJSON()
}
